// 处理权限路由模块
import { constantRoutes, asyncRoutes } from '@/router'

const state = {
  routes: constantRoutes // 当前用户所能访问的所有路由
}
const mutations = {
  setRoutes(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
const actions = {
  filterRoutes(context, menus) {
    // 筛选出当前用户所能访问的动态路由
    const newRoutes = []
    menus.forEach(key => {
      newRoutes.push(...asyncRoutes.filter(item => item.name === key))
    })
    context.commit('setRoutes', newRoutes)
    return newRoutes // 这里的return是给路由addRoutes用的
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
